package com.qitianyong.sgkusbcamera;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.gkusbsdk.GKUSBCommand;
import com.gkusbsdk.GKUSBFireware;
import com.gkusbsdk.GKUSBNormal;
import com.gkusbsdk.GKUSBRecordResolution;
import com.gkusbsdk.GKUSBRecordTime;
import com.gkusbsdk.GKUSBSensor;
import com.qitianyong.selfclass.BUS_MESSAGE;
import com.qitianyong.selfclass.BaseActivity;
import com.qitianyong.selfclass.CONSTANT;
import com.qitianyong.selfclass.GKDevice;
import com.qitianyong.selfclass.LogUtils;
import com.qitianyong.selfclass.Message2EventBus;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ConfigActivity extends BaseActivity {

    // UI
    private Button __btn_back;
    private RelativeLayout __rlResolution,__rlRecordTime,__rlSensor,__rlAboutDev;
    private TextView __tv_resolution_value, __tv_recordtime_value, __tv_sensor_value, __tv_dev_version, __tv_app_version;
    private ToggleButton __tb_auto, __tb_audio;

    private int __resolution_index, __sensor_index, __recordtime_index;
    private int __setting_index = 0;
    private int __getVersionErrCount = 20;

    private int clickAboutTimes = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);

        findView();
        setListener();

        // 请求当前设备信息
        GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RESOLUTION, null);
        GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORD_TIME, null);
        GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_SENSOR, null);
        GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORD_AUTO_MANAL, null);
        GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORD_AUDIO, null);
        GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORDER_VERSION, null);
    }

    private void findView(){

        __btn_back = (Button)findViewById(R.id.btn_back);
        __rlResolution = (RelativeLayout)findViewById(R.id.relative_resolution);
        __rlResolution.setEnabled(false);//默认不可更改分辨率 获取到产品类型后再打开
        __rlRecordTime = (RelativeLayout)findViewById(R.id.relative_recordtime);
        __rlSensor = (RelativeLayout)findViewById(R.id.relative_sensor);
        __rlAboutDev = (RelativeLayout)findViewById(R.id.relative_aboutdevice);

        __tv_resolution_value = (TextView)findViewById(R.id.tv_resolution_value);
        __tv_recordtime_value = (TextView)findViewById(R.id.tv_recordtime_value);
        __tv_sensor_value = (TextView)findViewById(R.id.tv_sensor_value);

        __tb_auto = (ToggleButton)findViewById(R.id.tb_auto);
        __tb_audio = (ToggleButton)findViewById(R.id.tb_audio);

        __tv_dev_version = (TextView)findViewById(R.id.tv_dev_version);

        __tv_app_version = (TextView)findViewById(R.id.tv_app_version);
        __tv_app_version.setText(BuildConfig.VERSION_NAME);
    }

    private void setListener(){

        __btn_back.setOnClickListener(mListener);
        __rlResolution.setOnClickListener(mListener);
        __rlRecordTime.setOnClickListener(mListener);
        __rlSensor.setOnClickListener(mListener);
        __rlAboutDev.setOnClickListener(mListener);

        __tb_auto.setOnClickListener(mListener);
        __tb_audio.setOnClickListener(mListener);

        findViewById(R.id.relative_aboutapp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickAboutTimes++;
                if(clickAboutTimes>8){
                    clickAboutTimes=0;
                    new AlertDialog.Builder(ConfigActivity.this)
                            .setItems(LogUtils.getAllLogs(),null)
                            .show();
                }
            }
        });

    }

    private View.OnClickListener mListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LogUtils.addClickLog(v.getId());
            switch(v.getId()){
                case R.id.btn_back:{
                    finish();
                    break;
                }
                case R.id.relative_resolution:{
                    Intent inItem = new Intent(ConfigActivity.this, ItemActivity.class);
                    inItem.putExtra("type", CONSTANT.ITEM_TYPE.TYPE_RESOLUTION);
                    inItem.putExtra("index", __resolution_index);
                    startActivityForResult(inItem, CONSTANT.ITEM_TYPE.TYPE_RESOLUTION);
                    break;
                }
                case R.id.relative_recordtime:{
                    Intent inItem = new Intent(ConfigActivity.this, ItemActivity.class);
                    inItem.putExtra("type", CONSTANT.ITEM_TYPE.TYPE_RECORDTIME);
                    inItem.putExtra("index", __recordtime_index);
                    startActivityForResult(inItem, CONSTANT.ITEM_TYPE.TYPE_RECORDTIME);
                    break;
                }
                case R.id.relative_sensor:{
                    Intent inItem = new Intent(ConfigActivity.this, ItemActivity.class);
                    inItem.putExtra("type", CONSTANT.ITEM_TYPE.TYPE_SENSOR);
                    inItem.putExtra("index", __sensor_index);
                    startActivityForResult(inItem, CONSTANT.ITEM_TYPE.TYPE_SENSOR);
                    break;
                }
                case R.id.relative_aboutdevice:{
                    __getVersionErrCount = 20;
                    GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORDER_VERSION, null);
                    break;
                }
                case R.id.tb_auto:{
                    GKUSBNormal normal = new GKUSBNormal();
                    if(__tb_auto.isChecked()){// 发送选中
                        normal.__normal = GKUSBCommand.SWITCH.OPEN;
                    }else{
                        normal.__normal = GKUSBCommand.SWITCH.CLOSE;
                    }

                    GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_SET_RECORD_AUTO_MANAL, normal);
                    break;
                }
                case R.id.tb_audio:{
                    GKUSBNormal normal = new GKUSBNormal();
                    if(__tb_audio.isChecked()){// 发送选中
                        normal.__normal = GKUSBCommand.SWITCH.OPEN;
                    }else{
                        normal.__normal = GKUSBCommand.SWITCH.CLOSE;
                    }

                    GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_SET_RECORD_AUDIO, normal);
                    break;
                }
                default:break;
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(data == null) return;
        int pos = data.getIntExtra("item", 0);
        __setting_index = pos;

        switch(requestCode){
            case CONSTANT.ITEM_TYPE.TYPE_RESOLUTION:{

                GKUSBRecordResolution resolution  = new GKUSBRecordResolution();
                resolution.__resolution = pos;
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_SET_RESOLUTION,
                        resolution);
                break;
            }
            case CONSTANT.ITEM_TYPE.TYPE_RECORDTIME:{

                GKUSBRecordTime recordtime = new GKUSBRecordTime();
                recordtime.__record_time = pos;
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_SET_RECORD_TIME,
                        recordtime);
                break;
            }
            case CONSTANT.ITEM_TYPE.TYPE_SENSOR:{

                GKUSBSensor sensor= new GKUSBSensor();
                sensor.__lever = pos;
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_SET_SENSOR,
                        sensor);
                break;
            }
            default:break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Message2EventBus message){

        switch(message.__what){
            case BUS_MESSAGE.BUS_GET_RESOLUTION:{

                int result = (int)message.object;
                if(result == GKUSBCommand.RESULT_RESOLUTION.P720){

                    __tv_resolution_value.setText(getResources()
                            .getStringArray(R.array.resolution_array)[1]);
                    __resolution_index = 1;
                }else if(result == GKUSBCommand.RESULT_RESOLUTION.P1080){
                    __tv_resolution_value.setText(getResources()
                            .getStringArray(R.array.resolution_array)[0]);
                    __resolution_index = 0;
                }

                Log.i("ConfigActivity", "resolution:"+result);
                break;
            }
            case BUS_MESSAGE.BUS_GET_RECORD_TIME:{
                int result = (int)message.object;
                if(result == GKUSBCommand.RESULT_RECORDTIME.ONE_MIN){

                    __tv_recordtime_value.setText(getResources()
                            .getStringArray(R.array.record_time_array)[0]);
                    __recordtime_index = 0;
                }else if(result == GKUSBCommand.RESULT_RECORDTIME.THREE_MIN){
                    __tv_recordtime_value.setText(getResources()
                            .getStringArray(R.array.record_time_array)[1]);
                    __recordtime_index = 1;
                }/*else if(result == GKUSBCommand.RESULT_RECORDTIME.FIVE_MIN){
                    __tv_recordtime_value.setText(getResources()
                            .getStringArray(R.array.record_time_array)[2]);
                    __recordtime_index = 2;
                }*/
                Log.i("ConfigActivity", "record time:"+result);
                break;
            }
            case BUS_MESSAGE.BUS_GET_SENSOR:{
                int result = (int)message.object;
                if(result == GKUSBCommand.RESULT_LEVER.HIGH){

                    __tv_sensor_value.setText(getResources()
                            .getStringArray(R.array.sensor_array)[2]);
                    __sensor_index = 2;
                }else if(result == GKUSBCommand.RESULT_LEVER.MIDDLE){
                    __tv_sensor_value.setText(getResources()
                            .getStringArray(R.array.sensor_array)[1]);
                    __sensor_index = 1;
                }else if(result == GKUSBCommand.RESULT_LEVER.LOW){
                    __tv_sensor_value.setText(getResources()
                            .getStringArray(R.array.sensor_array)[0]);
                    __sensor_index = 0;
                }

                Log.i("ConfigActivity", "sensor:"+result);
                break;
            }
            case BUS_MESSAGE.BUS_GET_RECORD_AUTO_MANAL:{
                int result = (int)message.object;
                if(result == GKUSBCommand.SWITCH.OPEN){
                    __tb_auto.setChecked(true);
                }else{
                    __tb_auto.setChecked(false);
                }

                Log.i("ConfigActivity", "auto_manal:"+result);
                break;
            }
            case BUS_MESSAGE.BUS_GET_RECORD_AUDIO:{
                int result = (int)message.object;
                if(result == GKUSBCommand.SWITCH.OPEN){
                    __tb_audio.setChecked(true);
                }else{
                    __tb_audio.setChecked(false);
                }

                Log.i("ConfigActivity", "audio_manal:"+result);
                break;
            }
            case BUS_MESSAGE.BUS_GET_RECORDER_VERSION_ERR:{
                if(__getVersionErrCount == 0) {
                    messsageBox(getString(R.string.version_err));
                    Log.i("version", "get ver err");
                }else{
                    __getVersionErrCount --;
                    GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORDER_VERSION, null);
                    Log.i("version", "__getVersionErrCount:"+__getVersionErrCount);
                }
                break;
            }
            case BUS_MESSAGE.BUS_GET_RECORDER_VERSION_OK:{

                GKUSBFireware fireware = (GKUSBFireware)message.object;
                if(fireware != null){
                    __rlAboutDev.setEnabled(false);
                    String version = new String(fireware.__version);
                    //version = version.substring(0,version.length()-1); // 蔡翔发布设备版本不需去掉‘\n’
                    __tv_dev_version.setText(version);
                    if(fireware.__product_type == GKUSBCommand.PRODUCT_TYPE.GK8602_A){
                        __rlResolution.setEnabled(true);
                    }
                }
                break;
            }
            case BUS_MESSAGE.BUS_SET_RECORD_AUTO_MANAL:{
                int result = (int)message.object;
                if(result == -1){
                    if(__tb_auto.isChecked()){
                        __tb_auto.setChecked(false);
                    }else{
                        __tb_auto.setChecked(true);
                    }
                }else{
                    if(__tb_auto.isChecked()){
                        messsageBox(getString(R.string.tip_auto_open));
                    }else{
                        messsageBox(getString(R.string.tip_auto_close));
                    }
                }
                break;
            }
            case CONSTANT.COMMAND.CMD_SET_RECORD_AUDIO:{
                int result = (int)message.object;
                if(result == -1){
                    if(__tb_audio.isChecked()){
                        __tb_audio.setChecked(false);
                    }else{
                        __tb_audio.setChecked(true);
                    }
                }else{
                    if(__tb_audio.isChecked()){
                        messsageBox(getString(R.string.tip_audio_open));
                    }else{
                        messsageBox(getString(R.string.tip_audio_close));
                    }
                }
                break;
            }
            case BUS_MESSAGE.BUS_SET_RESOLUTION:{
                int result = (int)message.object;
                if(result == -1){
                    messsageBox(getString(R.string.resolution_set_err));
                }else{
                    __tv_resolution_value.setText(getResources().
                            getStringArray(R.array.resolution_array)[__setting_index]);

                    __resolution_index =  __setting_index;
                }
                break;
            }
            case BUS_MESSAGE.BUS_SET_RECORD_TIME:{
                int result = (int)message.object;
                if(result == -1){
                    messsageBox(getString(R.string.recordtime_set_err));
                }else{
                    __tv_recordtime_value.setText(getResources().
                            getStringArray(R.array.record_time_array)[__setting_index]);

                    __recordtime_index = __setting_index;
                }
                break;
            }
            case BUS_MESSAGE.BUS_SET_SENSOR:{
                int result = (int)message.object;
                if(result == -1){
                    messsageBox(getString(R.string.sensor_set_err));
                }else{
                    __tv_sensor_value.setText(getResources().
                            getStringArray(R.array.sensor_array)[__setting_index]);

                    __sensor_index = __setting_index;
                }
                break;
            }
            default:break;
        }
    }



    private void messsageBox(String message){
        if(__toast != null){
            __toast.cancel();
        }

        __toast = Toast.makeText(ConfigActivity.this, message, Toast.LENGTH_SHORT);
        __toast.show();
    }
}
